package com.mm.cloth;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;

public class RegisterActivity extends AppCompatActivity {
    private EditText etPhone;
    private EditText etUsername;
    private EditText etPassword;
    private EditText etConfirmPassword;
    private Button btnRegister;
    private TextView tvLogin;
    private TextView tvUserAgreement;
    private TextView tvPrivacyPolicy;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        
        initViews();
        setupListeners();
    }

    private void initViews() {
        etPhone = findViewById(R.id.etPhone);
        etUsername = findViewById(R.id.etUsername);
        etPassword = findViewById(R.id.etPassword);
        etConfirmPassword = findViewById(R.id.etConfirmPassword);
        btnRegister = findViewById(R.id.btnRegister);
        tvLogin = findViewById(R.id.tvLogin);
        tvUserAgreement = findViewById(R.id.tvUserAgreement);
        tvPrivacyPolicy = findViewById(R.id.tvPrivacyPolicy);
    }

    private void setupListeners() {
        // 注册按钮点击事件
        btnRegister.setOnClickListener(v -> handleRegister());

        // 登录链接点击事件
        tvLogin.setOnClickListener(v -> finish());

        // 用户协议点击事件
        tvUserAgreement.setOnClickListener(v -> {
            Toast.makeText(this, "用户协议", Toast.LENGTH_SHORT).show();
            // TODO: 跳转到用户协议页面
        });

        // 隐私政策点击事件
        tvPrivacyPolicy.setOnClickListener(v -> {
            Toast.makeText(this, "隐私政策", Toast.LENGTH_SHORT).show();
            // TODO: 跳转到隐私政策页面
        });
    }

    private void handleRegister() {
        String phone = etPhone.getText().toString().trim();
        String username = etUsername.getText().toString().trim();
        String password = etPassword.getText().toString().trim();
        String confirmPassword = etConfirmPassword.getText().toString().trim();

        // 表单验证
        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(this, "请输入手机号", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(username)) {
            Toast.makeText(this, "请输入昵称", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(password)) {
            Toast.makeText(this, "请输入密码", Toast.LENGTH_SHORT).show();
            return;
        }

        if (password.length() < 6) {
            Toast.makeText(this, "密码长度不能少于6位", Toast.LENGTH_SHORT).show();
            return;
        }

        if (!password.equals(confirmPassword)) {
            Toast.makeText(this, "两次输入的密码不一致", Toast.LENGTH_SHORT).show();
            return;
        }

        // TODO: 调用注册API
        // 这里模拟注册成功
        Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();

        // 跳转到登录页面并传递手机号
        Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
        intent.putExtra("phone", phone);
        startActivity(intent);
        finish();
    }
} 